/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.stop.manage.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.stop.manage.dto.ClosedDriveStopAccessRecordDTO;
import com.hopes.bizz.stop.manage.dto.ClosedDriveStopAccessRecordExcelDTO;
import com.hopes.bizz.stop.manage.entity.ClosedDriveStopAccessRecordEntity;
import com.hopes.bizz.stop.manage.mapper.ClosedDriveStopAccessRecordMapper;
import com.hopes.bizz.stop.manage.service.ClosedDriveStopAccessRecordService;
import com.hopes.bizz.stop.manage.vo.ClosedDriveStopAccessRecordExcelVO;
import com.hopes.bizz.stop.manage.vo.ClosedDriveStopAccessStatisticsVO;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 停车场进出纪录
 *
 * @author pig
 * @date 2024-02-19 16:31:35
 */
@Service
public class ClosedDriveStopAccessRecordServiceImpl extends ServiceImpl<ClosedDriveStopAccessRecordMapper, ClosedDriveStopAccessRecordEntity> implements ClosedDriveStopAccessRecordService {

    @Override
    public ValidateImportBO<ClosedDriveStopAccessRecordExcelDTO> validateImport(List<ClosedDriveStopAccessRecordExcelDTO> excelDTOList) {
        ValidateImportBO<ClosedDriveStopAccessRecordExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<ClosedDriveStopAccessRecordExcelDTO> saveList = new ArrayList<>();
        int successNum = 0;
        int failedNum = 0;

        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public ClosedDriveStopAccessRecordEntity importDtoToEntity(ClosedDriveStopAccessRecordExcelDTO excelDto) {
        ClosedDriveStopAccessRecordEntity entity = new ClosedDriveStopAccessRecordEntity();
        BeanUtils.copyProperties(excelDto, entity);
        return entity;
    }

    @Override
    public ClosedDriveStopAccessRecordExcelVO exportEntityToVo(ClosedDriveStopAccessRecordEntity entity) {
        ClosedDriveStopAccessRecordExcelVO excelVO = new ClosedDriveStopAccessRecordExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        return excelVO;
    }

    @Override
    public ClosedDriveStopAccessStatisticsVO inOutStatistics(ClosedDriveStopAccessRecordDTO closedDriveStopAccessRecordDTO) {
        ClosedDriveStopAccessStatisticsVO closedDriveStopAccessStatisticsVO = new ClosedDriveStopAccessStatisticsVO();
        ArrayList<String> timeList = new ArrayList<>();
        closedDriveStopAccessStatisticsVO.setTimeList(timeList);
        ArrayList<Long> countList = new ArrayList<>();
        closedDriveStopAccessStatisticsVO.setCountList(countList);
        // 进出类型
        LocalDate beginTime = closedDriveStopAccessRecordDTO.getBeginTime();
        LocalDate endTime = closedDriveStopAccessRecordDTO.getEndTime();
        if (ObjectUtil.isNull(beginTime) || ObjectUtil.isNull(endTime)) {
            // 默认查询一个月
            closedDriveStopAccessRecordDTO.setEndTime(LocalDate.now());
            closedDriveStopAccessRecordDTO.setBeginTime(DateUtil.lastMonth().toLocalDateTime().toLocalDate());
        }
        Map<String, Map<String, Long>> statistics = baseMapper.statisticsByDirection(closedDriveStopAccessRecordDTO);
        statistics.forEach((k,v)->{
            timeList.add(k);
            countList.add(v.get("count"));
        });

        return closedDriveStopAccessStatisticsVO;
    }
}
